/*
Analysis of Pew Research Center data on American views of Chinese and international student enrollment

Paul Musgrave
paul.musgrave@gmail.com
*/

********************************************************************************
*								Installations								   *
********************************************************************************

/* You may have to install

ssc install cleanplots
ssc install coefplot
ssc install revrs
ssc install estout
ssc install esttab

*/ 

********************************************************************************
*								Stata Cruft									   *
********************************************************************************

clear all

version 18

set more off

macro drop _all

set scheme cleanplots

global MyDocs "~/Dropbox/0001 Academic Projects/Completed/"

global MyProject "${MyDocs}0171 Parasecurity and Education/Replication"

log using "${MyProject}/LOG Analysis Pew 2025 09 22", replace

********************************************************************************
*								Data Load and Clean							   *
********************************************************************************

use "${MyProject}/Pew Data Minimal.dta"

/* The dataset provided is truncated. The original is available from the Roper
Center, study 31118243, "Pew Research Center: American Trends Panel Wave 82",
February 1-7, 2021 */

// Create age category variable, removing missing values coded as 99

clonevar agecat = F_AGECAT
recode agecat (99=.)

// Recode China power containment priority variable

clonevar prcpower = GAP21_35
lab var prcpower "Containing power and influence of China"
recode prcpower (99=.)
recode prcpower (2/3=2)
lab def GAP21_35 2 "No/Some priority", modify

// Label the US-China relationship variable
lab var GAP21Q40 "China relationship with U.S."
recode GAP21Q40 (99=.)

// Create binary variable for views on admitting Chinese students to universities
clonevar goodbaduni = GAP21Q46
lab var goodbaduni "Good or bad to admit Chinese students" 
lab def goodbadunilab 0 "Bad" 1 "Good"
recode goodbaduni (99=.) (2=0)
lab val goodbaduni goodbadunilab

// Recode party identification into three categories
clonevar pid3 = F_PARTY_
recode pid3 (2=12) (3/4=2) (99=.)
recode pid3 (12 = 3)
lab def pid3lab 1 "Republican" 2 "Independent" 3 "Democrat"
lab val pid3 pid3lab

// Set up gender variable (keeping original coding)
clonevar gender_trinary = F_GENDER
recode gender_trinary (99=.)
lab def F_GENDER 1 "Male" 3 "Non-binary / other definition", modify

// Education level variable
clonevar education = F_EDUC_1
recode education (99=.)
lab var education "Education Level"

// Create binary indicator for born in USA
gen borninusa = (F_BIRTHP == 1)
replace borninusa = . if F_BIRTHP == 99
lab var borninusa "Born in USA"

// Race/ethnicity and household income variables
clonevar raceethn = F_RACETH
clonevar hhi_cat = F_INC_TI

// Create variable for support/opposition to limiting Chinese students
clonevar supportopposeprc = GAP21Q47
lab var supportopposeprc "View on limiting Chinese students"
recode supportopposeprc (99 = .)
revrs supportopposeprc, replace 

// Modify education label for clarity
lab def F_EDUC_1 5 "College grad/some postgrad", modify

// Create average thermometer rating across Asian countries (China, India, Japan, North Korea)
egen avgtherm = rowmean(thermchi thermind thermjap thermnko) if thermchi <= 100 & ///
				thermind <= 100 & thermjap <= 100 & thermnko <= 100

// Calculate net thermometer ratings (country rating minus average)
gen china_net_mean = thermchi - avgtherm
gen japan_net_mean = thermjap - avgtherm
lab var china_net_mean "China net thermometer"

// Create friendliness/enemy relationship variable	(DTR with China)			
clonevar frenemy = GAP21Q40
lab var frenemy "Describe US-China Relationship"


********************************************************************************
*							Descriptive Statistics							   *
********************************************************************************


dtable i.agecat i.gender_trinary i.raceethn i.education i.pid3 					///
,	title("Demographics For Pew Survey \label{tab:summarypew}")					///
	export("${MyProject}/APPENDIX Table A2.tex", replace tableonly) 

********************************************************************************
*							Regression Analysis							   *
********************************************************************************

* * * * * Support for International Students (Dichotomous DV)

* * * * OLS for ease of interpretation

// Model 1: Base model predicting support for international students
reg goodbaduni i.pid3 i.agecat b2.gender_trinary i.education borninusa i.raceethn, robust
est sto m1
local m1_N = e(N)

// Model 2: Add China power containment priority variable
reg goodbaduni i.pid3 i.agecat  b2.gender_trinary i.education borninusa i.raceethn  b2.prcpower, robust
est sto m2
local m2_N = e(N)

// Model 2 with survey weights
reg goodbaduni i.pid3 i.agecat  b2.gender_trinary i.education borninusa i.raceethn  b2.prcpower [aweight = WEIGHT_W], robust
est sto m2_weight
local m2_N = e(N)

// Model 3a: Include China net thermometer rating 
reg goodbaduni i.pid3 i.agecat  b2.gender_trinary i.education borninusa i.raceethn  china_net_mean if thermchi <= 100, robust
est sto m3a

// Model 3b: Include US-China relationship characterization
reg goodbaduni i.pid3 i.agecat  b2.gender_trinary i.education borninusa i.raceethn  i.frenemy, robust
est sto m3b

* * * * Logit for robustness

// Model 1L: Base logistic model predicting support for international students
logit goodbaduni i.pid3 i.agecat b2.gender_trinary i.education borninusa i.raceethn, robust
est sto m1L
local m1L_N = e(N)

// Model 2L: Add China power containment priority variable
logit goodbaduni i.pid3 i.agecat b2.gender_trinary i.education borninusa i.raceethn b2.prcpower, robust
est sto m2L
local m2L_N = e(N)

// Model 3aL: Include China net thermometer rating 
logit goodbaduni i.pid3 i.agecat b2.gender_trinary i.education borninusa i.raceethn china_net_mean if thermchi <= 100, robust
est sto m3aL

// Model 3bL: Include US-China relationship characterization
logit goodbaduni i.pid3 i.agecat b2.gender_trinary i.education borninusa i.raceethn i.frenemy, robust
est sto m3bL


/*
Not displayed

esttab m1 m1L m3a m3aL m3b m3bL, lab star(+ 0.10 * 0.05) nobase noomit 


*/

* * * * * * Support / Oppose Limiting Chinese Students (1 through 4)
	
	
ologit supportopposeprc i.pid3 i.agecat  b2.gender_trinary i.education borninusa i.raceethn , robust
est sto m4
local m4_N = e(N)


ologit supportopposeprc i.pid3 i.agecat  b2.gender_trinary i.education borninusa i.raceethn b2.prcpower, robust
est sto m5
local m5_N = e(N)


ologit supportopposeprc i.pid3 i.agecat  b2.gender_trinary i.education borninusa i.raceethn china_net_mean if thermchi < 100, robust
est sto m5a


ologit supportopposeprc i.pid3 i.agecat  b2.gender_trinary i.education borninusa i.raceethn i.frenemy, robust
est sto m5b

********************************************************************************
*							Visualization									   *
********************************************************************************

* * * * * * Subgraph A / Panel A

coefplot (m1, label(Base, N = `m1_N')) (m2, label(Priority, N = `m2_N'))  ///
	,xline(0) drop(_cons) coeflabels(, labsize(vsmall)) ///
	headings(	2.pid3 = "{bf:Party ID}"				///
				2.agecat = "{bf:Age}"					///
				1.gender_trinary = "{bf:Gender}"		///
				borninusa = "{bf:Nativity}"		///
				2.raceethn = "{bf:Race/Ethnicity}"		///
				2.education = "{bf:Education}"			///
				2.hhi_cat = "{bf:Household Income}"		///
				1.prcpower = "{bf:Limiting PRC Power Priority}" ///
				1.GAP21Q40 = "{bf:PRC Relationship with USA}" ///
	)													///
	xtitle("{it:OLS Coefficients}", size(vsmall))						///
	title("{bf:A:} {it:Support for International Student Enrollment}"	///
		, pos(11) span justification(left)) 			///
	subtitle("{stSerif:Do you think it's good or bad for U.S. colleges and universities to accept international students?}"						///
	, size(vsmall) pos(11) justification(left) span margin(0 0 2 0)) ///
	text(10 .2 "{bf:Support}""international""students", size(small)) ///
	text(14 -.2 "{bf:Oppose}""international""students", size(small)) ///
	xsize(8) ysize(10) ///
	legend(pos(11) ring(0) size(vsmall)) ///
///	caption("{stSerif:Pew Research Center, February 2021}", span size(small)) 	///
	name(g_general, replace) ///
	addplot(pcarrowi 8.5 .1 8.5 .35, color(gray) || pcarrowi 12.5 -.1 12.5 -.35, color(gray))

* * * * * * Subgraph B / Panel B

coefplot (m4, label(Base, N = `m4_N')) (m5, label(Priority, N = `m5_N')) ///
	,xline(0) drop(_cons) ///
	headings(	2.pid3 = "{bf:Party ID}"				///
				2.agecat = "{bf:Age}"					///
				1.gender_trinary = "{bf:Gender}"		///
				borninusa = "{bf:Nativity}"		///
				2.raceethn = "{bf:Race/Ethnicity}"		///
				2.education = "{bf:Education}"			///
				2.hhi_cat = "{bf:Household Income}"		///
				1.prcpower = "{bf:Limiting PRC Power Priority}" ///
	)													///
	xtitle("{it:Ordinal Logistic Coefficients}", size(vsmall))						///
	title("{bf:B:} {it:Limiting Chinese Students in U.S. Universities}"	///
		, pos(11) span justification(left)) 			///
	subtitle("{stSerif:When it comes to whether or not to limit Chinese students studying in the U.S., do you ...}" 					///
	, size(vsmall) pos(11) justification(left) span margin(0 0 2 0)) ///
	legend(pos(1) ring(0) size(vsmall)) ///
	text(15 1 "{bf:Support}""limiting" "Chinese""students", size(small)) ///
	text(22 -1.25 "{bf:Oppose}""limiting""Chinese""students", size(small)) ///
	coeflabels(, labsize(vsmall))	///
	name(g_prc, replace) ///
	addplot(pcarrowi 12.75 .4 12.75 1.6, color(gray) || pcarrowi 24.25 -.65 24.25 -1.85, color(gray))

* * * * * * Combine and Export Final Picture
	
gr combine g_general g_prc, rows(1) ///
	note("Priority models includes views on whether containing the power and influence of China should be a top priority or not. Question asked of half sample.""Robust standard errors and 95 percent confidence intervals shown.", span size(vsmall)) ///
	caption("{stSerif:Pew Research Center, American Trends Panel Wave 82, February 2021}", span size(vsmall))
	
gr export "${MyProject}/PAPER Figure 1.pdf", replace
	

* * * * * * Appendix Table
	
esttab m1 m2 m3a m3b m4 m5 m5a m5b using "${MyProject}/APPENDIX Table A3.tex"		///
	, replace longtable lab nobase noomit compress nogap						///
	title("Pew Full Results for International Enrollment \label{tab:pewresults}") ///
	nonotes addnote(Robust standard errors.)									///
	drop(*cons* *cut*)															///
	b(%9.2f) se(%9.2f)															///
	star(+ 0.10 * 0.05)															///
refcat(	2.pid3 "\textit{Party ID}"				///
		1.gender_trinary "\textit{Gender}"		///
		borninusa "\textit{Nativity}"			///
					2.agecat  "\textit{Age}"					///
	2.raceethn "\textit{Race/Ethnicity}"	///
		2.education "\textit{Education}"		///
		2.hhi_cat "\textit{Household Income}"	///
		1.prcpower "\textit{Limiting PRC Power Priority}" 	///
		, nolabel)	///
		mtitle("International" "International" "International" "International" ///
			"PRC" "PRC" "PRC" "PRC")

log close
